package com.un.ebs.sys.controller;

import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.sys.dto.ObjectParentAndIndexChangeDto;
import com.un.ebs.sys.dto.PageClassAddDto;
import com.un.ebs.sys.dto.PageClassUpdateDto;
import com.un.ebs.sys.service.PageClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2020-04-19
 */
@RestController
@RequestMapping("/pageClass")
@Api(value = "sys-page-class", tags = "")
public class PageClassController {

    @Autowired
    private PageClassService service;

    @PostMapping("add")
    @ApiOperation(value = "添加")
    public WebResult save(@RequestBody PageClassAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "修改")
    public WebResult update(@RequestBody PageClassUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public WebResult delete(@PathVariable String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "详情")
    public WebResult getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "查询")
    public WebResult list(@RequestParam(required = false) String classId) {
        return WebUtils.success(service.queryByParentId(classId));
    }

    @GetMapping("listAll")
    @ApiOperation(value = "查询")
    public WebResult listAll() {
        return WebUtils.success(service.queryAll());
    }

    @PostMapping("changeParent")
    @ApiOperation(value = "修改 parentId")
    public WebResult changeParent(@RequestParam String id, @RequestParam String parentId) {
        service.changeParent(id, parentId);
        return WebUtils.success();
    }

    @GetMapping("/child/{parentId}")
    @ApiOperation(value = "查询子类")
    public WebResult listChild(@PathVariable("parentId") String parentId) {
        return WebUtils.success(service.queryByParentId(parentId));
    }

    @PostMapping("changeSort")
    @ApiOperation(value = "单笔排序")
    public WebResult changeSort(@RequestParam String id, @RequestParam int index) {
        service.changeSort(id, index);
        return WebUtils.success();
    }

    @PostMapping("changeNodes")
    @ApiOperation("批量排序")
    public WebResult changeNodes(@RequestBody List<ObjectParentAndIndexChangeDto> req) {
        service.changeNode(req);
        return WebUtils.success();
    }

}